<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="到访人员邮箱" prop="email" label-width="100px">
        <el-input
          v-model="queryParams.email"
          placeholder="请输入邮箱"
          clearable
          @keyup.enter.native="handleQuery"
        />
        </el-form-item>
        
      <!-- <el-form-item label="部门编号" prop="depId"> -->
        <!-- <el-input
          v-model="queryParams.depId"
          placeholder="请输入部门编号"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
      <!-- </el-form-item> -->

<!-- 部门改下拉框 -->      
       <el-form-item label="部门名称" prop="depId">
          <el-select v-model="queryParams.depId" placeholder="请选择">
            <el-option
              v-for="item in department"
              :key="item.depId"
              :label="item.depName"
              :value="item.depId">
            </el-option>
          </el-select>
       </el-form-item>

      <!-- <el-form-item label="员工编号" prop="empId">
        <el-input
          v-model="queryParams.empId"
          placeholder="请输入员工编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->

<!-- 部门改下拉框 -->
      <el-form-item label="员工姓名" prop="empId">
        <el-select v-model="queryParams.empId" placeholder="请选择">
            <el-option
              v-for="item in employee1"
              :key="item.empId"
              :label="item.empName"
              :value="item.empId">
            </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          round
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['official:receive:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          round
          icon="el-icon-edit"
          size="mini" 
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['official:receive:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini" round
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['official:receive:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini" round
          @click="handleExport"
          v-hasPermi="['official:receive:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="receiveList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="编号" align="center" prop="id" /> -->
      <el-table-column label="到访人员邮箱" align="center" prop="email" />
      <el-table-column label="部门名称" align="center" prop="depName" />
      <el-table-column label="员工名称" align="center" prop="empName" />
      <el-table-column label="到访人员" align="center" prop="workPeople" />
      <!-- <el-table-column label="审核状态" align="center" prop="audit" /> -->
      
      <el-table-column label="到访时间" align="center" prop="visitTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.visitTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
           {{scope.row.updateTime==null?'暂无更新时间':''}}
        </template>
      </el-table-column>
      <el-table-column label="审核状况" align="center" prop="audit">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.audit==1">通过</el-tag>
          <el-tag type="danger" v-if="scope.row.audit==2">未通过</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['official:receive:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['official:receive:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改接待管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <!-- <el-form-item label="部门编号" prop="depId">
          <el-input v-model="form.depId" placeholder="请输入部门编号" />
        </el-form-item> -->
        <!--        部门编号改下拉框-->
        <el-form-item label="部门名称" prop="depId">
          <el-select v-model="form.depId" placeholder="请选择">
            <el-option
              v-for="item in department"
              :key="item.depId"
              :label="item.depName"
              :value="item.depId">
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="员工编号" prop="empId">
          <el-input v-model="form.empId" placeholder="请输入员工编号" />
        </el-form-item> -->
        
<!--        员工编号改下拉框-->
        <el-form-item label="员工姓名" prop="empId">
          <el-select v-model="form.empId" placeholder="请选择">
            <el-option
              v-for="item in employee1"
              :key="item.empId"
              :label="item.empName"
              :value="item.empId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="到访人员" prop="workPeople">
          <el-input v-model="form.workPeople" placeholder="请输入到访人员" />
        </el-form-item>
        <el-form-item label="到访时间" prop="visitTime">
          <el-date-picker clearable
            v-model="form.visitTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择到访时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="审核情况" prop="audit">
          <el-select v-model="form.audit" placeholder="请选择">
            <!--          clearable-->
            <!--          @keyup.enter.native="handleQuery">-->
            <el-option
              v-for="item in audits"
              :key="item.audit"
              :label="item.auditName"
              :value="item.audit">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
           <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listReceive,
  getReceive,
  delReceive,
  addReceive,
  updateReceive
} from "@/api/official/receive";
import { listEmployee1 } from "@/api/personnel/employee";
import { listDepartment } from "@/api/personnel/department";
export default {
  name: "Receive",
  data() {
    return {
      department: [],
      employee1: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 接待管理表格数据
      receiveList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        email: null,
        depId: null,
        empId: null,
        workPeople: null,
        audit: null
      },
       audits: [
        { audit: 1, auditName: "通过" },
        { audit: 2, auditName: "未通过" }
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        depId: [
          { required: true, message: "部门名称不能为空", trigger: "blur" }
        ],
        empId: [
          { required: true, message: "员工名称不能为空", trigger: "blur" }
        ],
        audit : [
          { required: true, message: "审核情况不能为空", trigger: "blur" }
        ],
        workPeople: [
          { required: true, message: "到访人员不能为空", trigger: "blur" },
          {
            validator: function(rule, value, callback) {
              //校验中文的正则：/^[\u4e00-\u9fa5]{0,}$/
              // ^[a-zA-Z\u4e00-\u9fa5]+$
              if (/^[a-zA-Z\u4e00-\u9fa5]+$/.test(value) == false) {
                callback(new Error("请输入中文或英文"));
              } else {
                //校验通过
                callback();
              }
            },
            trigger: "change"
          }
        ],
        email: [
          { required: true, message: "邮箱不能为空", trigger: "blur" },

          {
            pattern: /\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}/,
            required: true,
            message: "请输入正确的邮箱格式",
            trigger: "blur"
          }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    // 发送请求员工姓名
    listEmployee1().then(response => {
      this.employee1 = response.rows;
      this.loading = false;
    });
    // 发送请求部门名称
    listDepartment().then(response => {
      this.department = response.rows;
      this.loading = false;
    });
  },
  methods: {
    /** 查询接待管理列表 */
    getList() {
      this.loading = true;
      listReceive(this.queryParams).then(response => {
        this.receiveList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        email: null,
        depId: null,
        empId: null,
        workPeople: null,
        audit: null,
        visitTime: null,
        createTime: null,
        updateTime: null,
        version: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加接待管理";
      // 发送请求
      // listEmployee1().then(response => {
      //   this.employee1 = response.rows;
      //   this.loading = false;
      // });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getReceive(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改接待管理";
        // 发送请求
        // listEmployee1().then(response => {
        //   this.employee1 = response.rows;
        //   this.loading = false;
        // });
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateReceive(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addReceive(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除接待管理编号为"' + ids + '"的数据项？')
        .then(function() {
          //return delReceive(ids);
        })
        .then(() => {
          //删除逻辑
          let res = delReceive(ids);
          res.then(res => {
            if (res) {
              this.getList();
              this.$modal.msgSuccess("删除成功");
            } else {
              this.$modal.msgError("到访时间未达到，删除失败");
            }
          });

          // this.getList();
          // this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "official/receive/export",
        {
          ...this.queryParams
        },
        `receive_${new Date().getTime()}.xlsx`
      );
    }
  }
};
</script>
